package com.hdax.dm.mapstruct;

import com.hdax.dm.dto.OrderDto;
import com.hdax.dm.entity.order.DmOrder;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Mapper(componentModel = "spring")
public abstract class OrderDtoMapper {

    protected String calcTime(LocalDateTime createdTime){
        LocalDateTime endTime = createdTime.plusMinutes(15L);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return dateTimeFormatter.format(endTime);
    }

    @Mappings({
            @Mapping(source = "createdTime",target = "sellTime"),
            @Mapping(source = "totalCount",target = "num"),
            @Mapping(source = "insuranceAmount",target = "unitPrice"),
            @Mapping(target = "endTime", expression = "java(calcTime(dmOrder.getCreatedTime()))")
    })
    public  abstract OrderDto toTarget(DmOrder dmOrder);

    public  abstract List<OrderDto> toTargets(List<DmOrder> dmOrders);

}
